﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LedCenter.GUI
{
    public delegate void OnStopHandler(string name);
    public partial class DeviceProgressControl : UserControl
    {
        
        public DeviceProgressControl()
        {
            InitializeComponent();
        }

        public event OnStopHandler OnStoped;

        public void setInfo(string info)
        {
            lbName.Text = info;
        }

        void ProgressChanged1(object sender, ProgressChangedEventArgs e)
        {
            updateProcessThreadsafe(e.ProgressPercentage);
        }

        private void updateProcessThreadsafe(int percentage)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new MethodInvoker(
                    delegate() { updateProcessThreadsafe(percentage); }));
            }
            else
            {
                progressBar.Value = percentage;
            }
        }

        /// <summary>
        /// On completed do the appropriate task
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ProgressCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // The background process is complete. We need to inspect
            // our response to see if an error occurred, a cancel was
            // requested or if we completed successfully.  
            if (e.Cancelled)
            {
                lbName.Text = Name + ": Task Cancelled.";
            }

            // Check to see if an error occurred in the background process.

            else if (e.Error != null)
            {
                lbName.Text = Name + ": Error while sending data background operation.";
            }
            else
            {
                // Everything completed normally.
                lbName.Text = Name + ": Task Completed...";
            }
        }

        /// <summary>
        /// Notification is performed here to the progress bar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

            // This function fires on the UI thread so it's safe to edit

            // the UI control directly, no funny business with Control.Invoke :)

            // Update the progressBar with the integer supplied to us from the

            // ReportProgress() function.  

            progressBar.Value = e.ProgressPercentage;
            lbName.Text = Name + ": Sending......" + progressBar.Value.ToString() + "%";
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            OnStoped(this.Name);
        }
    }
}
